package com.xunshangwang.pinhaohuo.adapters;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.entity.Goods;

import java.text.DecimalFormat;

import tool.library.adapter.BaseRecyclerAdapter;

/**
 * Created by xzp on 16/1/15.
 */
public class GoodsAdapter extends BaseRecyclerAdapter<Goods>{
    private DecimalFormat mDecimalFormat;
    public GoodsAdapter(Context context) {
        super(context);
        mDecimalFormat=new DecimalFormat("0.00");
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(getContext()).inflate(R.layout.goods_item,parent,false);
        return new GoodsHolder(view);
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
        GoodsHolder goodsHolder= (GoodsHolder) holder;
        Goods goods=getList().get(position);
        Glide.with(getContext()).load(goods.getImage_url()).centerCrop().into(goodsHolder.goodsImage);
        goodsHolder.alonePriceText.setText("￥"+mDecimalFormat.format(goods.getAlone_price()));
        goodsHolder.GroupPriceText.setText("￥"+mDecimalFormat.format(goods.getGroup_price()));
        goodsHolder.goodsNameText.setText(goods.getGoods_name());
        goodsHolder.groupNumText.setText(String.valueOf(goods.getGroup_number()));

    }
    protected class GoodsHolder extends ClickableViewHolder{
        ImageView goodsImage;
        TextView groupNumText,goodsNameText,GroupPriceText,alonePriceText;
        public GoodsHolder(View itemView) {
            super(itemView);
            goodsImage= (ImageView) itemView.findViewById(R.id.goods_image);
            ImageView groupArrowImage= (ImageView) itemView.findViewById(R.id.right_arrow_icon);
            groupArrowImage.setColorFilter(ContextCompat.getColor(getContext(),R.color.white));
            groupNumText= (TextView) itemView.findViewById(R.id.goods_group_num_text);
            goodsNameText= (TextView) itemView.findViewById(R.id.goods_name_text);
            GroupPriceText= (TextView) itemView.findViewById(R.id.goods_group_price_text);
            alonePriceText= (TextView) itemView.findViewById(R.id.goods_alone_price_text);
            alonePriceText.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            alonePriceText.getPaint().setAntiAlias(true);
        }

        @Override
        public void onClick(View v) {
            super.onClick(v);
        }
    }
}
